﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;

namespace EasyNet
{
    public class Mail
    {
        private string serverAddress, senderAddress, senderPassword, receiverAddress, senderName, title, body;
        private int serverPort;
        static public void Send(MailServer mail_server, string sender_address, string sender_password, string receiver_address, string sender_name, string title_, string body_)
        {
            MailMessage msg = new System.Net.Mail.MailMessage();
            msg.To.Add(receiver_address); //收件人

            //发件人信息
            msg.From = new MailAddress(sender_address, sender_name, System.Text.Encoding.UTF8);
            msg.Subject = title_;   //邮件标题
            msg.SubjectEncoding = System.Text.Encoding.UTF8;    //标题编码
            msg.Body = body_; //邮件主体
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = true;  //是否HTML
            msg.Priority = MailPriority.High;   //优先级
            SmtpClient client = new SmtpClient();

            //设置邮箱和密码
            client.Credentials = new System.Net.NetworkCredential(sender_address, sender_password);
            switch (mail_server)
            {
                case MailServer.QQMail:
                    client.Port = 587;
                    client.Host = "smtp.qq.com";
                    break;
                case MailServer.Netease163Mail:
                    client.Port = 25;
                    client.Host = "smtp.163.com";
                    break;
            }
            client.EnableSsl = true;
            client.Send(msg);
        }
        static public void Send(string server_address, int server_port, string sender_address, string sender_password, string receiver_address, string sender_name, string title_, string body_)
        {
            MailMessage msg = new System.Net.Mail.MailMessage();
            msg.To.Add(receiver_address); //收件人

            //发件人信息
            msg.From = new MailAddress(sender_address, sender_name, System.Text.Encoding.UTF8);
            msg.Subject = title_;   //邮件标题
            msg.SubjectEncoding = System.Text.Encoding.UTF8;    //标题编码
            msg.Body = body_; //邮件主体
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = true;  //是否HTML
            msg.Priority = MailPriority.High;   //优先级
            SmtpClient client = new SmtpClient();

            //设置邮箱和密码
            client.Credentials = new System.Net.NetworkCredential(sender_address, sender_password);
            client.Host = server_address;
            client.Port = server_port;
            client.EnableSsl = true;
            client.Send(msg);
        }
        public void Send()
        {
            MailMessage msg = new System.Net.Mail.MailMessage();
            msg.To.Add(ReceiverAddress); //收件人

            //发件人信息
            msg.From = new MailAddress(SenderAddress, SenderName, System.Text.Encoding.UTF8);
            msg.Subject = Title;   //邮件标题
            msg.SubjectEncoding = System.Text.Encoding.UTF8;    //标题编码
            msg.Body = Body; //邮件主体
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = true;  //是否HTML
            msg.Priority = MailPriority.High;   //优先级
            SmtpClient client = new SmtpClient();

            //设置邮箱和密码
            client.Credentials = new System.Net.NetworkCredential(SenderAddress, SenderPassword);
            client.Host = ServerAddress;
            client.Port = ServerPort;
            client.EnableSsl = true;
            client.Send(msg);
        }
        public Mail(MailServer mail_server, string sender_address, string sender_password, string receiver_address, string sender_name, string title_, string body_)
        {
            switch (mail_server)
            {
                case MailServer.QQMail:
                    ServerAddress = "smtp.qq.com";
                    ServerPort = 465;
                    break;
                case MailServer.Netease163Mail:
                    ServerAddress = "smtp.163.com";
                    ServerPort = 25;
                    break;
            }
            SenderAddress = sender_address;
            SenderPassword = sender_password;
            ReceiverAddress = receiver_address;
            SenderName = sender_name;
            Title = title_;
            Body = body_;
        }
        public Mail(string server_address, int server_port, string sender_address, string sender_password, string receiver_address, string sender_name, string title_, string body_)
        {
            ServerAddress = server_address;
            ServerPort = server_port;
            SenderAddress = sender_address;
            SenderPassword = sender_password;
            ReceiverAddress = receiver_address;
            SenderName = sender_name;
            Title = title_;
            Body = body_;
        }
        public string ServerAddress
        {
            get
            {
                return serverAddress;
            }
            set
            {
                serverAddress = value;
            }
        }
        public int ServerPort
        {
            get
            {
                return serverPort;
            }
            set
            {
                serverPort = value;
            }
        }
        public string SenderAddress
        {
            get
            {
                return senderAddress;
            }
            set
            {
                senderAddress = value;
            }
        }
        public string SenderPassword
        {
            get
            {
                return senderPassword;
            }
            set
            {
                senderPassword = value;
            }
        }
        public string ReceiverAddress
        {
            get
            {
                return receiverAddress;
            }
            set
            {
                receiverAddress = value;
            }
        }
        public string SenderName
        {
            get
            {
                return senderName;
            }
            set
            {
                senderName = value;
            }
        }
        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
            }
        }
        public string Body
        {
            get
            {
                return body;
            }
            set
            {
                body = value;
            }
        }
    }
}
